package com.guigu.www.erp.dehao.controller;

import com.guigu.www.erp.common.pojo.PageVo;
import com.guigu.www.erp.dehao.pojo.User;
import com.guigu.www.erp.dehao.pojo.UserRole;
import com.guigu.www.erp.dehao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/User")
public class UserController {

    @Autowired
    private UserService service;

    @DeleteMapping("{userId}")
    public void deleteByPrimaryKey(@PathVariable Integer userId) {
        service.deleteByPrimaryKey(userId);
    }

    @PostMapping("/add")
    public void insertSelective(@RequestBody User user) {
        int roleId=Integer.valueOf(user.getUserImg());
        user.setUserImg("profile.jpg");
        user.setUserStatus(1);
        service.insertSelective(user);
        int userId=user.getUserId();
        UserRole userRole = new UserRole(null, userId, roleId);
        service.insert(userRole);
    }

    @GetMapping("{userId}")
    public User selectByPrimaryKey(@PathVariable Integer userId) {
        return service.selectByPrimaryKey(userId);
    }

    @PutMapping("/up")
    public void updateByPrimaryKeySelective(@RequestBody User user) {
        service.updateByPrimaryKeySelective(user);
    }

    @PostMapping("/login")
    public User login(User user,HttpServletRequest request) throws Exception{
        User login = service.login(user);

        if (login!=null) {
            request.getSession().setAttribute("loginUser",login);
            request.getSession().setAttribute("loginUserId",login.getUserId());
        }

        return login;
    }
    @GetMapping("/is")
    public User accountIsExist(String userAccount) {
        return service.accountIsExist(userAccount);
    }

    @RequestMapping("/find1")
    public PageVo<User> findAllUser1(Integer pageNum, Integer pageSize, String userName) {
        return service.findAllUser1(pageNum, pageSize, userName);
    }

    @RequestMapping("/find0")
    public PageVo<User> findAllUser0(Integer pageNum,Integer pageSize,String userName) {
        return service.findAllUser0(pageNum, pageSize, userName);
    }
    @RequestMapping("/all")
    public List<User> findAll() {
        return service.findAll();
    }

}
